//
//  MyController.m
//  MyTrip
//
//  Created by zhang on 15/8/22.
//  Copyright (c) 2015年 张益. All rights reserved.
//

#import "MyController.h"
#import "DKNightVersion.h"
#import "MyCell.h"
#import "SaveController.h"
#import "WeController.h"

@interface MyController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain)  UITableView *table;


@end

@implementation MyController

- (void)dealloc
{
    [_table release];
    [super dealloc];
}


- (void)createTable{
    
    self.table = [[[UITableView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain] autorelease];
    _table.delegate = self;
    _table.dataSource = self;
    [_table registerClass:[MyCell class] forCellReuseIdentifier:@"cell"];
    _table.separatorStyle = UITableViewCellSeparatorStyleNone;
    _table.nightBackgroundColor = UIColorFromRGB(0x343434);
    _table.nightSeparatorColor =  UIColorFromRGB(0x313131);
    self.navigationController.navigationBar.nightBarTintColor = UIColorFromRGB(0x444444);
    self.navigationItem.leftBarButtonItem.nightTintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem.nightTintColor = [UIColor whiteColor];
    
    _table.bounces = NO;

    [self.view addSubview:_table];
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 100;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 4;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.row == 0) {
        cell.myLabel.text = @"我的收藏";
        cell.myImage.image = [UIImage imageNamed:@"mysave.png"];
    }else if (indexPath.row == 1){
        cell.myLabel.text = @"清空缓存";
        cell.myImage.image = [UIImage imageNamed:@"clear.png"];
    }else if (indexPath.row == 2){
        cell.myLabel.text = @"夜间模式";
        cell.myImage.image = [UIImage imageNamed:@"night.png"];
    }else if (indexPath.row == 3){
        cell.myLabel.text = @"关于我们";
        cell.myImage.image = [UIImage imageNamed:@"women"];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 2) {
//        if ([DKNightVersionManager currentThemeVersion] == DKThemeVersionNormal) {
//            [DKNightVersionManager nightFalling];
//            
//        }else{
//            [DKNightVersionManager dawnComing];
//        }
        if (self.view.window.alpha == 1) {
            self.view.window.alpha = 0.5;
        }else{
            self.view.window.alpha = 1;
        }
        
        
        
        
        
    }else if (indexPath.row == 0){
        SaveController *save = [[SaveController alloc] init];
        [self.navigationController pushViewController:save animated:YES];
        [save release];
    }else if (indexPath.row == 1){
        UIAlertController *alter = [UIAlertController alertControllerWithTitle:@"提示" message:@"清空缓存" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            NSFileManager *manger = [NSFileManager defaultManager];
            NSString *directoryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) firstObject];
            NSString *path = [directoryPath stringByAppendingPathComponent:@"myCaches"];
            NSLog(@"%@", path);
            
            if ([manger fileExistsAtPath:path]) {
                 long size=[manger attributesOfItemAtPath:path error:nil].fileSize;
                NSString *str = [NSString stringWithFormat:@"清空缓存%ld K", size];
                NSLog(@"%@", str);
                [manger removeItemAtPath:path error:nil];
                
                UIAlertController *clearAlter = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *clear = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    
                }];
                [clearAlter addAction:clear];
                [self presentViewController:clearAlter animated:YES completion:^{
                    
                }];
            }else{
                NSString *str = [NSString stringWithFormat:@"清空缓存0 K"];
                NSLog(@"%@", str);
                [manger removeItemAtPath:path error:nil];
                
                UIAlertController *clearAlter = [UIAlertController alertControllerWithTitle:@"提示" message:str preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction *clear = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                    
                }];
                [clearAlter addAction:clear];
                [self presentViewController:clearAlter animated:YES completion:^{
                    
                }];
            }
        }];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            
            
        }];
        [alter addAction:okAction];
        [alter addAction:cancelAction];
        [self  presentViewController:alter animated:YES completion:^{
            
        }];
    }else if (indexPath.row == 3){
        WeController *we = [[WeController alloc] init];
        [self.navigationController pushViewController:we animated:YES];
        [we release];
        
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:70 / 255.0f green:160 / 255.0f blue:175 / 255.0f alpha:1];
    
    self.navigationItem.title = @"设置";
    [self createTable];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
